package com.fit.dao.registration;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fit.entities.LicencePlate;
import com.fit.entities.User;

public class LicencePlateDAOImpl implements LicencePlateDAO {

	@Inject
	private Session session;

	public boolean save(LicencePlate registration) {
		if (registration != null) {
			session.save(registration);
			return true;
		}
		return false;
	}

	public LicencePlate findByLicencePlateNo(String licencePlateNo) {
		ArrayList<LicencePlate> list = (ArrayList<LicencePlate>) session.createCriteria(LicencePlate.class).add(Restrictions.eq("licencePlateNo",licencePlateNo)).list();
		if(list.size()==0){
			return null;
		} 
		return list.get(0);
	}
	
	@SuppressWarnings("unchecked")
	public List<LicencePlate> findAll() {
		return session.createCriteria(LicencePlate.class).list();
	}

	public boolean update(LicencePlate registration) {
		if (registration != null) {
			session.update(registration);
			return true;
		}
		return false;
	}

	public boolean deleteByLicencePlateNo(String licencePlateNo) {
		LicencePlate r = findByLicencePlateNo(licencePlateNo);
		if (r != null) {
			session.delete(r);
			return true;
		}
		return false;
	}


}
